﻿using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using TfsComLib.TfsBranchLib2;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Interaction logic for BranchListEditBox.xaml
    ///     Control to select list of branches
    /// </summary>
    public partial class BranchListEditBox : UserControl
    {
        private CollectionViewSource branchView;

        private ISelectedBranches context;

        public BranchListEditBox()
        {
            InitializeComponent();
        }

        public BranchListEditBox(ISelectedBranches context)
        {
            InitializeComponent();
            SetContext(context);
        }

        //private HighlightAdorner highlightAdorner;

        public void SetContext(ISelectedBranches context)
        {
            this.context = context;
            context.PropertyChanged += context_PropertyChanged;

            branchView = new CollectionViewSource {Source = context.Branches};
            branchView.View.Refresh();
            branchListBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding {Source = branchView});
        }

        private void context_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Branches")
            {
                if ((context.Branches == null) || (context.Branches.Count == 0))
                {
                    IsEnabled = false;
                }
                else
                {
                    IsEnabled = true;
                }
            }
        }

        private void ChangeSelected(bool? isChecked = null)
        {
            var selectedBranches = branchListBox.SelectedItems.Cast<BranchData>().Select(branchData => branchData);
            bool setValue;
            if (isChecked == null)
            {
                setValue = !(selectedBranches.All(branchData => branchData.IsSelected));
            }
            else
            {
                setValue = (bool) isChecked;
            }
            foreach (var branchData in selectedBranches)
            {
                branchData.IsSelected = setValue;
            }
        }

        private void ListBoxItem_PreviewKeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                ChangeSelected();
                e.Handled = true;
            }
        }

        private void branchListBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                ChangeSelected();
                e.Handled = true;
            }
        }

        protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
        {
            var item = (ListBoxItem) sender;
            item.IsSelected = true;
        }

        private void CheckBox_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem lvi = null;
            var temp = sender as DependencyObject;
            if (temp == null) return;
            var maxlevel = 0;
            while (!(temp is ListBoxItem) && maxlevel < 1000)
            {
                temp = VisualTreeHelper.GetParent(temp);
                maxlevel++;
            }
            lvi = temp as ListBoxItem;
            if (lvi != null)
            {
                if (lvi.IsSelected)
                {
                    var checkBox = sender as CheckBox;
                    ChangeSelected(!checkBox.IsChecked);
                    e.Handled = true;
                }
            }
        }
    }
}